/**
 * 定义一个响应式数据 每当从data的key中读取数据时mget函数被触发,每当往data的key中设置数据时,set函数被触发
 * @param {*} data 
 * @param {*} key 
 * @param {*} val 
 */
import Dep from "./dep.js";
import { Observer } from "./observer.js";
let obj = {}
function defineReactive (data, key, val) {
  if (typeof val === 'object') new Observer(val)
  let dep = new Dep()
  let childOb = obserer(val)
  Object.defineProperty(data, key, {
    enumerable: true,
    configurable: true,
    get: function () {
      console.log('访问');
      dep.depend() //修改
      if (childOb) {
        childOb.dep.depend()
      }
      return val
    },
    set: function (newVal) {
      console.log('修改');
      if (val === newVal) return
      val = newVal
      // 通知所有依赖就行更新
      dep.notify()
    }
  })
  console.log(dep);
}
export function obserer (value, asRootData) {
  if (!isObject(value)) {
    return
  }
  let ob
  if (hasOwn(vale, '__ob__') && value.__ob__ instanceof Observer) {
    ob = value.__ob__
  } else {
    ob = new Observer(value)
  }
  return ob
}